﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using Yelper.API;

namespace Yelper.Phone {
    public static class SettingsFacade {
        private static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        private const string USERNAME = "Username";
        public static string Username {
            get {
                return settings.Contains(USERNAME) ? (string)settings[USERNAME] : null;                    
            }
            set {
                settings[USERNAME] = value;
            }
        }

        private const string PASSWORD = "Password";
        public static string Password {
            get {
                return settings.Contains(PASSWORD) ? (string)settings[PASSWORD] : null;
            }
            set {
                settings[PASSWORD] = value;
            }
        }

        private const string SELECTED_YELP = "SelectedYelp";
        public static Yelp SelectedYelp {
            get {
                return settings.Contains(SELECTED_YELP) ? (Yelp)settings[SELECTED_YELP] : null;
            }
            set {
                settings[SELECTED_YELP] = value;
            }
        }

        private const string SELECTED_USER = "SelectedUser";
        public static User SelectedUser {
            get {
                return settings.Contains(SELECTED_USER) ? (User)settings[SELECTED_USER] : null;
            }
            set {
                settings[SELECTED_USER] = value;
            }
        }

        private const string USERNAME_TEMP = "UsernameTemp";
        public static string UsernameTemp {
            get {
                return settings.Contains(USERNAME_TEMP) ? (string)settings[USERNAME_TEMP] : null;
            }
            set {
                settings[USERNAME_TEMP] = value;
            }
        }

        private const string PASSWORD_TEMP = "PasswordTemp";
        public static string PasswordTemp {
            get {
                return settings.Contains(PASSWORD_TEMP) ? (string)settings[PASSWORD_TEMP] : null;
            }
            set {
                settings[PASSWORD_TEMP] = value;
            }
        }

        public static void ClearCredentials() {
            if (settings.Contains(USERNAME)) {
                settings.Remove(USERNAME);
            }
            if (settings.Contains(PASSWORD)) {
                settings.Remove(PASSWORD);
            }

            if (settings.Contains(USERNAME_TEMP)) {
                settings.Remove(USERNAME_TEMP);
            }
            if (settings.Contains(PASSWORD_TEMP)) {
                settings.Remove(PASSWORD_TEMP);
            }
            settings.Save();
        }

        public static void Save() {
            settings.Save();
        }
    }
}
